//
//  RervsecodeViewController.m
//  02-Geocoder的使用
//
//  Created by apple on 15/1/30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "RervsecodeViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <AMapLocationKit/AMapLocationKit.h>
@interface RervsecodeViewController ()<CLLocationManagerDelegate,AMapLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager * manager;
@property (nonatomic,strong) AMapLocationManager * locationManager;
@property (nonatomic,copy) NSString * lonStr;
@property (nonatomic,copy) NSString * latStr;
@end

@implementation RervsecodeViewController


- (CLLocationManager *)manager{
    if (_manager == nil) {
        _manager = [[CLLocationManager alloc]init];
    }
    return _manager;
}

- (AMapLocationManager * )locationManager
{
    if (_locationManager == nil) {
        _locationManager = [[AMapLocationManager alloc]init];
        _locationManager.delegate = self;
    }
    return _locationManager;
}

//开始获取地址
- (void)locationGD
{
    
    [self.manager requestAlwaysAuthorization];
    
    if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {
        [self.manager requestWhenInUseAuthorization];
    }
    // 带逆地理信息的一次定位（返回坐标和地址信息）
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //   定位超时时间，可修改，最小2s
    self.locationManager.locationTimeout = 3;
    //   逆地理请求超时时间，可修改，最小2s
    self.locationManager.reGeocodeTimeout = 5;
    
    // 带逆地理（返回坐标和地址信息）
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        
        if (error)
        {
            if (error.code == 5)
            {
                return;
            }
        }
        
        if (regeocode)
        {
            NSString * addr = [NSString stringWithFormat:@"%@%@%@%@%@",regeocode.city,regeocode.district,regeocode.township,regeocode.street,regeocode.number];
            if ([self.locationDelegate respondsToSelector:@selector(updateLocation:)]) {
                [self.locationDelegate updateLocation:addr];
            }
        }
    }];


}


@end
